package com.doit;

import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

/**
 * @DATE 2021/12/10/16:58
 * @Author MDK
 * @Version 2021.2.2
 *      数据变化监听
 **/
public class C06_Watcher {
    public static void main(String[] args) throws Exception {
        ZooKeeper zk = new ZooKeeper("linux01:2181,linux02:2181,linux03:2181",2000,null);
        //获取值
        byte[] data = zk.getData("/a", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                try {
                    System.out.println("/a节点的数据发生变化..."+ event.getType());
                    byte[] data1 = zk.getData("/a",this, null);
                    System.out.println("更新后的值是"+ new String(data1));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },null);

        System.out.println("/a当前节点的值是"+ new String(data));
        Thread.sleep(Integer.MAX_VALUE);
    }
}
